package com.ruoyi.project.system.uflo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.uflo.model.ProcessDefinition;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.process.assign.Assignee;
import com.bstek.uflo.process.node.TaskNode;
import com.bstek.uflo.service.ProcessService;
import com.bstek.uflo.service.TaskOpinion;
import com.bstek.uflo.service.TaskService;
import com.ruoyi.common.utils.ObjectUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.system.uflo.mapper.UfloTaskMapper;
import com.ruoyi.project.system.uflo.model.dto.TodoTaskDTO;
import com.ruoyi.project.system.uflo.model.vo.CensorVO;
import com.ruoyi.project.system.uflo.model.vo.ExamineVO;
import com.ruoyi.project.system.uflo.service.IUfloTaskService;
import com.ruoyi.project.system.user.domain.User;

/**
 * task任务
 * @author xianz
 *
 */
@Service
public class UfloTaskService implements IUfloTaskService{

	@Autowired
	TaskService taskService;
	
	@Autowired
	ProcessService processService;
	
	@Autowired
	UfloTaskMapper ufloTaskMapper;
	
	/**
	 * 根据任务id及任务节点名称获取任务处理人列表
	 */
	@Override
	public List<Assignee> getTaskNodeAssignees(Long taskId, String taskNodeName) {
		Task task = taskService.getTask(taskId);
		ProcessInstance processInstance=processService.getProcessInstanceById(task.getProcessInstanceId());
		ProcessDefinition pd=processService.getProcessById(processInstance.getProcessId());
		TaskNode node=(TaskNode)pd.getNode(taskNodeName);
		List<Assignee> assignees = node.getAssignees();
		return assignees;
	}

	/**
	 * 送审
	 */
	@Override
	@Transactional
	public void censor(CensorVO censorVO) {
		
		Long taskId = censorVO.getTaskId();
		String nextStep = censorVO.getNextStep();
		String assignee = censorVO.getAssignee();
		String opinion = censorVO.getOpinion();
		
		//开始任务
		taskService.start(taskId);
		//设置下一步骤处理人
		taskService.saveTaskAppointor(taskId, assignee, nextStep);
		//完成任务并设置下一任务节点
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("taskPid", taskId);
		taskService.forward(taskId, nextStep,variables,new TaskOpinion(opinion));
	}

	/**
	 * 我的代办
	 */
	@Override
	public List<TodoTaskDTO> selectTodoList(TodoTaskDTO todoTaskDTO,User user) {
		
		todoTaskDTO.setAssignee(user.getLoginName());
		
		return ufloTaskMapper.selectUfloTaskList(todoTaskDTO);
	}

	/**
	 * 审核
	 */
	@Override
	public void examine(ExamineVO examineVO) {
		
		Long taskId = examineVO.getTaskId();
		
		if(examineVO.getExamine()) {
			
			if(ObjectUtils.isNotEmpty(examineVO.getNextStep())) {
				//审核通过
				CensorVO censorVO = new CensorVO();
				BeanUtils.copyPropertiesFilterNull(examineVO, censorVO, new String[] {});
				censor(censorVO);
			}else {
				//无下一步骤
				taskService.start(taskId);
				taskService.complete(taskId);
			}
		
		}else {
			//驳回
			//开始任务
			String nextStep = examineVO.getNextStep();
			String opinion = examineVO.getOpinion();
			taskService.start(taskId);
			taskService.rollback(taskId, nextStep, null, new TaskOpinion(opinion));
		}
		
	}

}
